package com.snopy.datastructure.list;

import lombok.extern.slf4j.Slf4j;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Spliterator;
import java.util.function.Consumer;

/**
 * @author snopy
 * @version 1.0.0
 * @classname SequenceList
 * @description
 * @email 77912204@qq.com
 * @date 2022/02/22 17:53
 */
@Slf4j
public class  SequenceList<E> implements MyList<E>,Iterable<E> {

    private final Object [] list;
    private int length;
    private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
    protected int modCount = 0;
    public SequenceList() {
        this.list = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
    }

    public void clear(){
        this.length = 0;
    }

    @Override
    public boolean isempty() {
        return this.length == 0;
    }

    @Override
    public int length() {
        return this.length;
    }

    @Override
    public E get(int i) {
        checkRange(i);
        return element(i);
    }

    E element(int i){
        return (E) list[i];
    }
    @Override
    public void insert(int i, E t) {
        if (i==length){
            throw new RuntimeException("当前表已满");
        }
        if (i<0||i>length){
            throw new RuntimeException("插入位置不合法");
        }
        for (int j = length; j > i; j--) {
            list[j] = list[j-1];
        }

        list[i] = t;
        length++;
    }

    @Override
    public void insert(E t) {
        list[length++] = t;
    }

    @Override
    public E remove(E t) {
        E res = null;
        int index = 0;
        for (int i = 0; i < length; i++) {
            if (list[i].equals(t)){
                index = i;
                res = (E) list[i];
            }
        }
        if (res!=null){
            for (int i=index;i<length-1;i++){
                list[i] = list[i+1];
            }
            length--;
        }
        return res;
    }

    @Override
    public int indexOf(E t) {
        for (int i = 0; i < length; i++) {
            if (list[i].equals(t)){
                return i;
            }
        }
        return -1;
    }

    private void checkRange(int i){
        if (i<0||i>length-1){
            throw new IndexOutOfBoundsException(outOfMessage(i));
        }
    }
    private String outOfMessage(int i){
        return "index"+i+"length"+length;
    }

    @Override
    public Iterator<E> iterator() {
        return new Itr();
    }

    @Override
    public void forEach(Consumer action) {
        Iterable.super.forEach(action);
    }

    @Override
    public Spliterator<E> spliterator() {
        return Iterable.super.spliterator();
    }
    private class Itr implements Iterator<E>{
        private int cursor;
        private int lastref = -1;
        private int expectedModCount = modCount;
        @Override
        public boolean hasNext() {
            return cursor<length;
        }

        @Override
        public E next() {
            int i = cursor;
            if (i>=length){
                throw new RuntimeException("");
            }
            E e = SequenceList.this.get(i);

            return (E) list[cursor];
        }

        @Override
        public void remove() {
            Iterator.super.remove();
        }

        @Override
        public void forEachRemaining(Consumer<? super E> action) {
            Iterator.super.forEachRemaining(action);
        }
    }

    public static void main(String[] args) {
        String[] arr = {"hello","world!","I","am","snopy！","nice","to","meet","you"};
        String[] arr2 = new String[3];
        System.arraycopy(arr,1,arr2,0,3);

        for (int i = 0; i < arr2.length; i++) {
            log.info("ele:{}",arr2[i]);
        }
    }
}
